const Router = require('koa-router')
const router = new Router()
const mongoose = require('mongoose')

//用户注册
router.post('/regist', async (ctx) => {
   try {
      const saveUser = mongoose.model("users")
      const newUser = saveUser(ctx.request.body)
      const result = await newUser.save()
      //必须要是异步的，要加async和await  exec（）不能用
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})


//游客登录
router.post('/login', async (ctx) => {
   try {
      const saveUser = mongoose.model("users")
      const loginUser = ctx.request.body
      const result = await saveUser.findOne(loginUser)
      if (result.userName!=="hellsink") {
         ctx.body = { code: 200, message: "登录成功" }
      }else if(result.userName=="hellsink"){
         ctx.body = {code:200,message:"这是管理员"}
      } 
   }
   catch (error) {
      ctx.body = { code: 200, message: "登录失败,未注册"}
   }
})

//随机获取注册用户头像
router.post('/getuserimg', async (ctx) => {
   try {
      const img = mongoose.model("userimgs")
      const getImg = ctx.request.body
      console.log(getImg);
      const result = await img.findOne(getImg)
      //必须要是异步的，要加async和await  exec（）不能用
      console.log(result);
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})

//获取博客详情接口
router.post('/gettest', async (ctx) => {
   try {
      const test = mongoose.model("blogs")
      const result = await test.findOne(ctx.request.body)
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})

//存储用户评论接口
router.post('/comments', async (ctx) => {
   try {
      const comment = mongoose.model("usercomments")
      const aaa = ctx.request.body
      const saveComment = new comment(aaa)
      await saveComment.save()
      console.log(saveComment);
      ctx.body = { code: 200, message: '评论成功' }
   }
   catch (error) {
      console.log(error + "---------------------");
   }
})

//获取博客详情评论内容接口
router.post('/getcomments', async (ctx) => {
   try {
      const comment = mongoose.model("usercomments")
      const findComment = ctx.request.body
      const result = await comment.find(findComment)
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})

//添加点赞数到数据库
router.post('/addagree', async (ctx) => {
   try {
      const blogs = mongoose.model("blogs")
      const title = ctx.request.body.title
      const agreeNum = ctx.request.body.agreeNum
      const result = await blogs.update({title:title},{$set:{agreeNum:agreeNum}},{multi:true})
      //这里使用了mongodb的按条件插入操作
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})

//添加阅读数到数据库
router.post('/addread', async (ctx) => {
   try {
      const blogs = mongoose.model("blogs")
      const title = ctx.request.body.title
      const readNum = ctx.request.body.readNum
      const result = await blogs.updateOne({title:title},{$set:{readNum:readNum}},{multi:true})
      //这里使用了mongodb的按条件插入操作
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})
//将回复添加到数据库
router.post('/replytodatabase', async (ctx) => {
   try {
      const usercomments = mongoose.model("usercomments")
      const content = ctx.request.body.content
      const content02 = ctx.request.body.content02
      const replyUserName = ctx.request.body.replyUserName
      const title = ctx.request.body.title
      
      const result = await usercomments.update({content:content,title:title}, {$push:{reply:{content02:content02,replyUserName:replyUserName}}});
      //这里使用了mongodb的按条件插入操作
      ctx.body = { code: 200, message: result }
   }
   catch (error) {
      console.log(error);
   }
})
module.exports = router